
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */
package com.hlkj.pay.service.common.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.provider.Aes256EncryptionProvider;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.common.constants.CommucationConstant;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.dto.common.MessageRecordQueryDto;
import com.hlkj.pay.infrastructure.mapper.common.MessageRecordMapper;
import com.hlkj.pay.infrastructure.model.common.MessageRecordDO;
import com.hlkj.pay.service.common.IMessageRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author monk@techsun.com
 * @date 2023/04/25 10:05
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MessageRecordServiceImpl implements IMessageRecordService {

    private final Aes256EncryptionProvider aes256EncryptionProvider;
    private final MessageRecordMapper messageRecordMapper;
    @Override
    public PageResult<MessageRecordDO> queryPage(MessageRecordQueryDto messageRecordQueryDto) {

        String receiver = messageRecordQueryDto.getReceiver();
        if (StringUtils.hasText(receiver)) {
            boolean wechat = false;
            if (CommonEnum.TEMPLATE_TYPE.WECHAT.getCode().equals(messageRecordQueryDto.getTemplateType())) {
                wechat = true;
            } else
                if (CollectionUtils.isNotEmpty(messageRecordQueryDto.getTemplateTypes())
                    && messageRecordQueryDto.getTemplateTypes().contains(CommonEnum.TEMPLATE_TYPE.WECHAT.getCode())) {
                        wechat = true;
                    }
            if (!wechat) {
                try {
                    messageRecordQueryDto.setReceiver(aes256EncryptionProvider.encryptString(receiver));
                } catch (Exception e) {
                    log.error("encrypt error:{}", e.getMessage(),e);
                }
            }
        }
        String sortingFields = messageRecordQueryDto.getSortingFields();
        if (StringUtils.isEmpty(sortingFields)) {
            sortingFields = CommucationConstant.FIELD_ID + StringPool.DASH;
        }
        else {
            if (sortingFields.contains(CommucationConstant.FIELD_CREATE_TIME)) {
                sortingFields = sortingFields.replace(CommucationConstant.FIELD_CREATE_TIME, CommucationConstant.FIELD_ID);
            }
        }
        messageRecordQueryDto.setSortingFields(sortingFields);
        return messageRecordMapper.selectPage(messageRecordQueryDto, buildWrapper(messageRecordQueryDto));
    }

    @Override
    public List<MessageRecordDO> queryList(MessageRecordQueryDto messageRecordQueryDto) {
        return messageRecordMapper.selectList(buildWrapper(messageRecordQueryDto));
    }

    @Override
    public MessageRecordDO detail(String messageId) {
            MessageRecordQueryDto messageRecordQueryDto = new MessageRecordQueryDto();
            messageRecordQueryDto.setMessageId(messageId);
            LambdaQueryWrapperX<MessageRecordDO> queryWrapperX = buildWrapper(messageRecordQueryDto);
            queryWrapperX.last(CommucationConstant.LIMIT_ONE);
            return messageRecordMapper.selectOne(queryWrapperX);
    }

    @Override
    public void addMessageRecord(MessageRecordDO messageRecordDO) {
        messageRecordMapper.insert(messageRecordDO);
    }

    @Override
    public void addMessageRecordList(List<MessageRecordDO> messageRecordList) {
        messageRecordMapper.insertBatch(messageRecordList);

    }

    @Override
    public void updateMessageRecordStatus(List<MessageRecordDO> messageList, CommonEnum.STATUS messageStatus) {
        Set<String> messageIdList = messageList.stream().map(MessageRecordDO::getMessageId).collect(Collectors.toSet());
        MessageRecordDO messageRecordDO = new MessageRecordDO();
        messageRecordDO.setStatus(messageStatus.getCode());
        MessageRecordQueryDto messageRecordQueryDto = new MessageRecordQueryDto();
        messageRecordQueryDto.setMessageIds(messageIdList);
        messageRecordMapper.update(messageRecordDO, buildWrapper(messageRecordQueryDto));
    }
    
    private LambdaQueryWrapperX<MessageRecordDO> buildWrapper(MessageRecordQueryDto queryDO) {
        LambdaQueryWrapperX<MessageRecordDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(MessageRecordDO::getTemplateCode, queryDO.getTemplateCode());
        queryWrapperX.eqIfPresent(MessageRecordDO::getReceiver, queryDO.getReceiver());
        queryWrapperX.eqIfPresent(MessageRecordDO::getTemplateType, queryDO.getTemplateType());
        queryWrapperX.inIfPresent(MessageRecordDO::getTemplateType, queryDO.getTemplateTypes());
        queryWrapperX.eqIfPresent(MessageRecordDO::getStatus, queryDO.getStatus());
        queryWrapperX.eqIfPresent(MessageRecordDO::getSendTimeType, queryDO.getSendTimeType());
        queryWrapperX.geIfPresent(MessageRecordDO::getSendTime, queryDO.getStartSendTime());
        queryWrapperX.geIfPresent(MessageRecordDO::getId, queryDO.getMinId());
        queryWrapperX.leIfPresent(MessageRecordDO::getSendTime, queryDO.getEndSendTime());
        return queryWrapperX;
    }
    
}
